﻿using Net_SCADA.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Net_SCADA
{
    public partial class Form_UserSet : fdBaseForm

    {
        int iOperateIndex;
        public Form_UserSet()
        {
            InitializeComponent();
            formLanguageIndex = -1;
        }

        private void btn_Close_Click(object sender , EventArgs e)
        {
            this.Dispose();
        }
        private void Form_UserSet_Load(object sender , EventArgs e)
        {
            cmb_Create_Right.Items.Add(1);
            cmb_Create_Right.Items.Add(2);
            cmb_Create_Right.Items.Add(3);
            cmb_Create_Right.Items.Add(4);
            cmb_Create_Right.SelectedIndex = 0;
        }

        private void btn_Message_OK_Click(object sender , EventArgs e)
        {

            if (iOperateIndex != 31) pl_User_Message.Visible = false;
            switch (iOperateIndex)
            {
                case 11:
                    pl_User_ChangePWD.Visible = true;
                    break;
                case 20:
                case 30:
                    pl_User_ManagerLogin.Visible = true;
                    break;
                case 21:
                    pl_User_CreateUser.Visible = true;
                    break;
                case 31:
                    fdUsers.iniDeleteUser(new StringBuilder(cmb_Delete_Username.Text));
                    fdMessage.ShowMsg(MultipleLanguage.messageInfos[24], cmb_Delete_Username.Text, formLanguageIndex);
                    //fdMessage.ShowMsg("所选用户已经被删除","删除用户",1,0,"用户", cmb_Delete_Username.Text);
                    //lbl_User_Message.Text = "用户 " + cmb_Delete_Username.Text + " 已经被删除！";
                    //btn_Message_Cancel.Visible = false;
                    iOperateIndex = 0;
                    break;
                default:
                    break;
            }
        }

        private void btn_Message_Cancel_Click(object sender , EventArgs e)
        {
            switch (iOperateIndex)
            {
                case 31:
                    pl_User_Delete.Visible = true;
                    break;
                default:
                    break;
            }
            pl_User_Message.Visible = false;
        }

        private void btn_Change_OK_Click(object sender , EventArgs e)
        {
            //检查输入情况
            if (txt_Change_NewPWD1.Text.Trim() == "" | txt_Change_NewPWD2.Text.Trim() == "" | txt_Change_OldPWD.Text.Trim() == "")
            {
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[25], "", formLanguageIndex);
                //ShowMSG(11 , new StringBuilder("必须输入旧密码和新密码！"));
                return; 
            }

            if (txt_Change_NewPWD2.Text.Trim() != txt_Change_NewPWD1.Text.Trim())
            {
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[26], "", formLanguageIndex);
                //ShowMSG(11 , new StringBuilder("两次输入的新密码必须一致！"));
                return;
            }

            //检查密码是否正确

            if (!fdUsers.iniCheckUser(new StringBuilder(cmb_Change_Username.Text) , new StringBuilder(txt_Change_OldPWD.Text)))
            {
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[27], "", formLanguageIndex);
                //ShowMSG(11 , new StringBuilder("必须正确输入旧密码！"));
                return;
            }
            else
            {
                //修改密码
                fdUsers.iniChangePWD(new StringBuilder(cmb_Change_Username.Text) , new StringBuilder(txt_Change_NewPWD1.Text));
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[28], cmb_Change_Username.Text, formLanguageIndex);

                //ShowMSG(0 , new StringBuilder("用户 " + cmb_Change_Username.Text + " 密码修改成功！"));
                pl_User_ChangePWD.Visible = false;
                txt_Change_NewPWD2.Text = "";
                txt_Change_NewPWD1.Text = "";
                txt_Change_OldPWD.Text = "";
                cmb_Change_Username.Items.Clear();
            }
        }

        private void btn_Change_Cancel_Click(object sender , EventArgs e)
        {
            pl_User_ChangePWD.Visible = false;
            txt_Change_NewPWD2.Text = "";
            txt_Change_NewPWD1.Text = "";
            txt_Change_OldPWD.Text = "";
            cmb_Change_Username.Items.Clear();
        }

        private void btn_Delete_OK_Click(object sender , EventArgs e)
        {
            DialogResult res=  fdMessage.ShowMsg(MultipleLanguage.messageInfos[29], cmb_Delete_Username.Text, formLanguageIndex);
            // ShowMSG(31 , new StringBuilder("确认要删除用户：" + cmb_Delete_Username.Text + " 吗？"));
            if (res.Equals(DialogResult.OK)){
                fdUsers.iniDeleteUser(new StringBuilder(cmb_Delete_Username.Text));
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[24], cmb_Delete_Username.Text, formLanguageIndex);
                pl_User_Delete.Visible = false;
            }
        }

        private void btn_Delete_Cancel_Click(object sender , EventArgs e)
        {
            pl_User_Delete.Visible = false;
        }

        private void btn_Change_Click(object sender , EventArgs e)
        {
            ListUses(cmb_Change_Username);
            pl_User_ChangePWD.Visible = true;
        }

        private void btn_Create_Click(object sender , EventArgs e)
        {
            iOperateIndex = 20;
            pl_User_ManagerLogin.Visible = true;
            txt_ManagerPWD.Text = "";
            txt_ManagerPWD.Focus();
        }

        private void btn_Delete_Click(object sender , EventArgs e)
        {
            iOperateIndex = 30;
            pl_User_ManagerLogin.Visible = true;
            txt_ManagerPWD.Text = "";
            txt_ManagerPWD.Focus();
        }

        private void btn_Create_OK_Click(object sender , EventArgs e)
        {
            if (txt_Create_Username.Text.Trim() == "")
            {
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[30], "", formLanguageIndex);
               // ShowMSG(21 , new StringBuilder("用户名不能为空，请输入用户名！"));
                //pl_User_CreateUser.Visible = false;
                return;
            }
            else
            {
                if (fdUsers.iniCheckUsername(new StringBuilder(txt_Create_Username.Text.Trim())))
                {
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[31], "", formLanguageIndex);
                    //ShowMSG(21 , new StringBuilder("用户名已经存在，请重新输入！"));
                   // pl_User_CreateUser.Visible = false;

                    return;
                }
            }
            if (txt_Create_NewPWD1.Text == "" | txt_Create_NewPWD2.Text == "")
            {
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[32], "", formLanguageIndex);
               // ShowMSG(21 , new StringBuilder("必须输入新建用户的密码！"));
                //pl_User_CreateUser.Visible = false;

                return;
            }
            if (txt_Create_NewPWD1.Text != txt_Create_NewPWD2.Text)
            {
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[33], "", formLanguageIndex);
                //ShowMSG(21 , new StringBuilder("两次输入的密码不同，请重新输入！"));
                //pl_User_CreateUser.Visible = false;

                return;
            }
            fdUsers.iniAddUser(new StringBuilder(txt_Create_Username.Text.Trim()) , new StringBuilder(txt_Create_NewPWD1.Text) , Convert.ToInt32(cmb_Create_Right.Text));
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[34], txt_Create_Username.Text.Trim(), formLanguageIndex);
           // ShowMSG(0 , new StringBuilder("新增加权限为 " + cmb_Create_Right.Text + " 的用户 " + txt_Create_Username.Text.Trim() + " ！"));
            pl_User_CreateUser.Visible = false;
            txt_Create_NewPWD1.Text = "";
            txt_Create_NewPWD2.Text = "";
            txt_Create_Username.Text = "";
            cmb_Create_Right.SelectedIndex = 0;
        }

        private void btn_Create_Cancel_Click(object sender , EventArgs e)
        {
            pl_User_CreateUser.Visible = false;
            txt_Create_NewPWD1.Text = "";
            txt_Create_NewPWD2.Text = "";
            txt_Create_Username.Text = "";
            cmb_Create_Right.SelectedIndex = 0;
        }

        private void btn_ManagerLogin_OK_Click(object sender , EventArgs e)
        {
            //检验管理员密码

            if (fdUsers.iniCheckManagerPWD(txt_ManagerPWD.Text))
            {
                if (iOperateIndex == 20)
                {
                    pl_User_CreateUser.Visible = true;
                }
                if (iOperateIndex == 30)
                {
                    pl_User_Delete.Visible = true;
                    ListUses(cmb_Delete_Username);
                }
                pl_User_ManagerLogin.Visible = false;
            }
            else
            {
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[35], "", formLanguageIndex);
                txt_ManagerPWD.Text = "";
                //ShowMSG(iOperateIndex , new StringBuilder("管理员密码输入错误！请重新输入"));
            }
           
        }

        private void btn_ManagerLogin_Cancel_Click(object sender , EventArgs e)
        {
            pl_User_ManagerLogin.Visible = false;
            txt_ManagerPWD.Text = "";
        }
        private void ShowMSG(int operateIndex , StringBuilder Message)
        {
            lbl_User_Message.Text = Message.ToString();
            iOperateIndex = operateIndex;
            switch (operateIndex)
            {
                case 11:
                    btn_Message_Cancel.Visible = false;
                    pl_User_ChangePWD.Visible = false;
                    break;
                case 20:
                case 30:
                    btn_Message_Cancel.Visible = false;
                    pl_User_ManagerLogin.Visible = true;
                    break;
                case 21:
                    btn_Message_Cancel.Visible = false;
                    break;
                case 31:
                    btn_Message_Cancel.Visible = true;
                    break;
                default:
                    btn_Message_Cancel.Visible = false;
                    break;
            }
            pl_User_Message.Visible = true;
        }
        private void ListUses(ComboBox cmbBox)
        {
            fdUsers.iniLoadUses();
            cmbBox.Items.Clear();
            for (int i = 0 ; i < fdUsers.UsersName.Count ; i++)
            {
                cmbBox.Items.Add(fdUsers.UsersName[i].ToString());
            }
            cmbBox.SelectedIndex = 0;
        }
    }
}
